package com.csu.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.csu.dao.MemberDao;
import com.csu.dao.MemberDaoImpl;

public class RegUI extends JFrame {

	RegUI(){
		
		//窗口
		this.setTitle("家庭财务管理系统 -- 注册");
		this.setSize(400,300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		
		//用户名面板
		ImageIcon acctImg = new ImageIcon("photo/AcctPic.png");
		JLabel acctLabel = new JLabel();
		acctLabel.setIcon(acctImg);
		acctLabel.setBounds(10, 10, 50, 50);
		JTextField acctField = new JTextField();
		acctField.setBounds(60, 20, 200, 30);
		JPanel panel1 = new JPanel();
		panel1.add(acctLabel);
		panel1.add(acctField);
		panel1.setLayout(null);
				
		//密码面板
		ImageIcon pwdImg = new ImageIcon("photo/PwdPic.png");
		JLabel pwdLabel = new JLabel();
		pwdLabel.setIcon(pwdImg);
		pwdLabel.setBounds(10, 10, 50, 50);
		JPasswordField pwdField = new JPasswordField();
		pwdField.setBounds(60, 20, 200, 30);
		JPanel panel2 = new JPanel();
		panel2.add(pwdLabel);
		panel2.add(pwdField);
		panel2.setLayout(null);

		//确认密码面板
		ImageIcon ConfirmPwdImg = new ImageIcon("photo/ConfirmPwd.png");
		JLabel ConfirmPwdLabel = new JLabel();
		ConfirmPwdLabel.setIcon(ConfirmPwdImg);
		ConfirmPwdLabel.setBounds(10, 10, 50, 50);
		JPasswordField ConfirmPwdField = new JPasswordField();
		ConfirmPwdField.setBounds(60, 20, 200, 30);
		JPanel panel5 = new JPanel();
		panel5.add(ConfirmPwdLabel);
		panel5.add(ConfirmPwdField);
		panel5.setLayout(null);
		
		//类型面板
		ImageIcon typeImg = new ImageIcon("photo/TypePic.png");
		JLabel typeLabel = new JLabel();
		typeLabel.setIcon(typeImg);
		
		typeLabel.setBounds(10, 10, 50, 50);
		JComboBox<String> typeBox = new JComboBox<String>();
		typeBox.addItem("管理员");
		typeBox.addItem("普通用户");
		typeBox.setSelectedItem("普通用户");
		typeBox.setBounds(60, 20, 200, 30);
		JPanel panel3 = new JPanel();
		panel3.add(typeLabel);
		panel3.add(typeBox);
		panel3.setLayout(null);
		
		//按钮
		JButton regBtn = new JButton("注册");
		JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panel4.add(regBtn);
		
		//注册按钮事件
		regBtn.addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberDao memberDao = new MemberDaoImpl();
				if(acctField.getText().equals("") || String.valueOf(pwdField.getPassword()).equals("") || String.valueOf(ConfirmPwdField.getPassword()).equals("")) {
				}
				else {
					if(memberDao.findUser(acctField.getText())==1 || !String.valueOf(pwdField.getPassword()).equals(String.valueOf(ConfirmPwdField.getPassword()))) {
						JOptionPane.showMessageDialog(null,
								"注册失败，可能由于以下原因导致："+'\n'+'\n'+
										"·用户名已存在"+'\n'+
										"·两次密码不一致",
								"提示",JOptionPane.ERROR_MESSAGE);
						acctField.requestFocus();
						acctField.selectAll();
						pwdField.setText("");
						ConfirmPwdField.setText("");
						typeBox.setSelectedItem("普通用户");
					}
					else {
						memberDao.insertUser(typeBox.getSelectedIndex(),acctField.getText(),String.valueOf(pwdField.getPassword()));
						JOptionPane.showMessageDialog(null,
								"注册成功！",
								"提示",JOptionPane.INFORMATION_MESSAGE);
						setVisible(false);
					}
				}
				
		   	}
		});
		
		//创建容器
		Box vBox = Box.createVerticalBox();
		vBox.add(panel1);
		vBox.add(panel2);
		vBox.add(panel5);
		vBox.add(panel3);
		vBox.add(panel4);
		
		//布局管理
		setLayout(null);
		add(vBox);
		vBox.setBounds(50,10,300,280);
		
		
	}
	
}
